
class UploadedtoPlugin:
	def __init__(self):
		# put all the links this plugins supports here - it will be used to filter supported URLs
		self.supported_sites = ['uploaded.to']
		self.supported_urls = self.supported_sites

	def get(self, url):
		"""Get a single file from www.uploaded.to"""
		post_pat = 'post" action="'
		titl_pat = '<title>'
		local_file = None
		remote_url = None
		first_try = True
		while True:
			data = urllib2.urlopen(url).read()
			account_blocked = False
			# Find the line with our file location
			for line in data.splitlines():
				if line.find(post_pat) != -1:
					remote_url = line.split(post_pat)[1].split('"')[0]
					break
				elif line.find(titl_pat) != -1:
					local_file = line.split(' ...')[0].split(titl_pat)[1].strip()
				elif line.find('Or wait 1 hour') != -1:
					account_blocked = True
					break
			if account_blocked:
				if first_try:
					config.report.report_warning('uploaded.to blocked us for an hour')
					for x in xrange(65 * 60):
						if config.stop_getting:
							config.stop_getting = False
							return
						time.sleep(1)
				else:
					raise CantDownload("Unable to download from uploaded.to even after 1 hour sleep")
			else:
				break
			first_try = False

		if not remote_url or not local_file:
			raise CantDownload('Error occured while trying to download')

		download_file_with_retries(local_file, remote_url, True)
		config.report.report_finish(local_file)

	def url_matches(self, url):
		"""Check whether we support this URL"""
		for l in self.supported_urls:
			if url.find(l) != -1:
				return True
		return False

	def version(self):
		"""Return plugin version - when interface changes, the version changes"""
		return 1

modules = [UploadedtoPlugin()]
